package com.bsh.controller.merchant;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.service.MerchantCategoryService;
import com.bsh.service.MerchantService;
import com.bsh.service.RegionService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class MerchantAskController {
	private static Logger log = LoggerFactory.getLogger(MerchantAskController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	MerchantService mercService;
	@Autowired
	RegionService regServ;
	@Autowired
	MerchantCategoryService merchantCateServ;
	@Autowired
	MerchantService merchantServ;

	/**
	 * 商家申请列表
	 */
	@RequestMapping(value = "/merchant/ask/list")
	public ModelAndView merchantAskList() {
		String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		String mobile = request.getParameter("mobile") == null ? null : request.getParameter("mobile").toString();
		Integer status = request.getParameter("status") == null ? -1
				: Integer.parseInt(request.getParameter("status").toString());

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/merchantAskList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = mercService.getAskPage(pager, userName, mobile, status);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("userName", userName);
		mv.addObject("mobile", mobile);
		mv.addObject("status", status);
		return mv;
	}

	/**
	 * 审核通过
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/audit/pass/{mid}")
	public PageJosnMessage pass(@PathVariable(value = "mid") Integer mid) {
		Integer res = mercService.audit(mid, true, null);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 审核未通过
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/audit/refuse")
	public ModelAndView refuse(@RequestParam(value = "mid") Integer mid, @RequestParam(value = "msg") String auditMsg) {
		Integer res = mercService.audit(mid, false, auditMsg);
		ModelAndView mv = refuseMsg(mid);
		mv.addObject("mid", mid);
		mv.addObject("msg", auditMsg);

		String msg = "";
		if (res == 1)
			msg = PageTools.alertCloseFraRefresh("审核商家申请", "审核成功！", 1000);
		else
			msg = PageTools.alert("审核商家申请", "审核失败，请稍后重试！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 添加审核消息
	 */
	@ResponseBody
	@RequestMapping(value = "/merchant/audit/msg/{mid}")
	public ModelAndView refuseMsg(@PathVariable(value = "mid") Integer mid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/merchant/auditMsg");
		mv.addObject("mid", mid);
		return mv;
	}
}
